package com.butuh.uang.tuh.ang.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.butuh.uang.tuh.ang.R;
import com.butuh.uang.tuh.ang.listener.OnClickZhanKaiListener;
import com.butuh.uang.tuh.ang.model.FaqModel;

import cn.droidlover.xdroidmvp.base.XListAdapter;

public class FAQAdapter extends XListAdapter<FaqModel> {

    OnClickZhanKaiListener onClickZhanKaiListener;

    public FAQAdapter(Context context, OnClickZhanKaiListener onClickZhanKaiListener) {
        super(context);
        this.onClickZhanKaiListener = onClickZhanKaiListener;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHloder viewHloder;
        if (convertView == null) {
            viewHloder = new ViewHloder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_faq, null);
            viewHloder.layout_status = convertView.findViewById(R.id.layout_status);
            viewHloder.iv_zhankai = convertView.findViewById(R.id.iv_zhankai);
            viewHloder.layout_zhankai = convertView.findViewById(R.id.layout_zhankai);
            viewHloder.tv_name = convertView.findViewById(R.id.tv_name);
            viewHloder.tv_message = convertView.findViewById(R.id.tv_message);
            convertView.setTag(viewHloder);
        } else {
            viewHloder = (ViewHloder) convertView.getTag();
        }
       final FaqModel model = data.get(position);
        viewHloder.tv_name.setText(model.getName());
        viewHloder.tv_message.setText(model.getMessage());
        if (model.isZhanKai()) {
            viewHloder.iv_zhankai.setImageResource(R.mipmap.faq_guanbi);
            viewHloder.layout_zhankai.setVisibility(View.VISIBLE);
        } else {
            viewHloder.iv_zhankai.setImageResource(R.mipmap.faq_zhankai);
            viewHloder.layout_zhankai.setVisibility(View.GONE);
        }
        viewHloder.layout_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickZhanKaiListener.onZhanKai(position, !model.isZhanKai());
            }
        });
        return convertView;
    }

    class ViewHloder {
        RelativeLayout layout_status;
        ImageView iv_zhankai;
        LinearLayout layout_zhankai;
        TextView tv_name;
        TextView tv_message;
    }

}
